﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;

namespace Web.EmailTemplate
{
    public partial class EditEmailTemplate : System.Web.UI.Page
    {
        static string prevPage = String.Empty;
        static string requestEdit=string.Empty;
        static EMAIL_TEMPLATE emailtemplate;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            requestEdit = Request.QueryString["id"];
            if (requestEdit != null && requestEdit.Trim() != "")
            {                           
                LoadEmailTemplateDetail();
            }
        }
        private void LoadEmailTemplateDetail()
        {
            long id = Convert.ToInt64(requestEdit);     
            emailtemplate = EmailTemplateService.GetEmailTemplateByID(id);
            if (emailtemplate != null)
            {
                txtDescription.Text = emailtemplate.DESCRIPTION;
                txtMessage.Text = emailtemplate.MESSAGE;
                txtSubject.Text = emailtemplate.SUBJECT;
                txtTemplateName.Text = emailtemplate.TEMPLATE_NAME;
            }
            else
            {
                divContent.Style["display"] = "none";
                divError.Style["display"] = "block";
            }
        }
        
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            EMAIL_TEMPLATE email = new EMAIL_TEMPLATE();
            email.ID = emailtemplate.ID;
            email.DESCRIPTION = (txtDescription.Text);
            email.MESSAGE = (txtMessage.Text);
            email.SUBJECT = (txtSubject.Text);
            email.TEMPLATE_NAME = (txtTemplateName.Text);
            int updateRe = EmailTemplateService.UpdateEmailTemplate(email);
            if (updateRe == 1)
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Update email template successful !" + "','" + "EmailTemplates.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Update email template successful !", "EmailTemplates.aspx");
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"showMessage('" + "Can not update this email template !" + "');");
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not update email template !');", true);
        }
    }
}